<?php
/**
 * Object Relational Persistence Mapping Class for table: prescription_template
 *
 * @package	com.clearhealth
 * @author	ClearHealth Inc.
 */
class PrescriptionTemplate extends ORDataObject {

	/**#@+
	 * Fields of table: prescription_template mapped to class members
	 */
	var $template_id		= '';
	var $template_name		= '';
	var $doctor_id		= '';
	var $date		= '';
	var $modified = '';
	var $note		= '';
	//var $dateformat = '%d/%m/%Y';
	/**#@-*/

	const LIMITED_COMMON_TEMPLATES = 5;
	
	/**
	 * DB Table
	 */
	var $_table = 'prescription_template';

	/**
	 * Primary Key
	 */
	var $_key = 'template_id';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'PrescriptionTemplate';

	/**
	 * Handle instantiation
	 */
	function PrescriptionTemplate() {
		parent::ORDataObject();
		
	}

	
	/**#@+
	 * Field: date, time formatting
	 */
	function get_date() {
		return $this->_getDate('date');
	}
	function set_date($date) {
		$this->_setDate('date',$date);
	}
	/**#@-*/
	
	function addNewTemplate($template){	
		$data = array (
				'doctor_id' 		=> $template['doctor_id'],
				'template_name'		=> $template['name'],
		        'date'				=> date('Y-m-d'),
		        'modified'			=> date('Y-m-d'),
				'note'				=> $template['description']
			);		
		$this->populateArray($data);		
		$this->persist();				
		return $this->template_id;			
	}
	
	function removeTemplate($templateID){
		$sql = "DELETE FROM prescription_template WHERE template_id = '$templateID'";
		$this->_execute($sql);		
		$sql = "DELETE FROM template_item WHERE template_id = '$templateID'";
		$this->_execute($sql);		
	}	

	function updateInfo($info){
		$sql = "UPDATE prescription_template SET ".
			$info['whichfield']. " = '".$info['new_value'] .
			"' WHERE template_id = '" .$info['id']."'";	
		$this->_execute($sql);
	}	
	
	function updateTemplate($template){		
		$sql = "UPDATE prescription_template SET 
					note = '"          . $template['description'] . "',
					template_name = '" . $template['name'] . "',
					modified = '"      . date('Y-m-d') . "'
				WHERE template_id = " . $template['template_id'] ;	
		$this->_execute($sql);
	}	
	
	function removeTemplateItems($templateId){
		$sql = "DELETE FROM template_item where template_id = '$templateId'";
		$this->_execute($sql);
	}
	
	function listTemplates($doctor_id){
		$ds = new Datasource_sql();
		$sql = array(
			'cols' 		=> "template_id, template_name, DATE_FORMAT(date,'".L10nHelper::getSqlDateFormat('Ymd')."') AS date, DATE_FORMAT(modified,'".L10nHelper::getSqlDateFormat('Ymd')."') AS modified, note",
			'from' 		=> "prescription_template", 			
			'where' 	=> "doctor_id = $doctor_id",
			'orderby'	=> "template_id"
			);
	
		$ds->setup($this->_db,$sql);
		$result = $ds->toArray();
		if (!empty($result)) return $result;
		return false;			
	}
	
	function listCommonTemplates($doctor_id){
		$ds = new Datasource_sql();
		$sql = array(
			'cols' 		=> "template_id, template_name, DATE_FORMAT(date,'".L10nHelper::getSqlDateFormat('Ymd')."') AS date, DATE_FORMAT(modified,'".L10nHelper::getSqlDateFormat('Ymd')."') AS modified, note",
			'from' 		=> "prescription_template", 			
			'where' 	=> "doctor_id = $doctor_id",
			'orderby'	=> "template_id",
			'limit'		=> self::LIMITED_COMMON_TEMPLATES
			);
	
		$ds->setup($this->_db,$sql);
		$result = $ds->toArray();
		if (!empty($result)) return $result;
		return false;			
	}
	
	function getTemplate($templateID){
		$ds = new Datasource_sql();
		$sql = array(
			'cols' 		=> "template_id, template_name, DATE_FORMAT(date,'".L10nHelper::getSqlDateFormat('Ymd')."') AS date, DATE_FORMAT(modified,'".L10nHelper::getSqlDateFormat('Ymd')."') AS modified, note",
			'from' 		=> "prescription_template", 			
			'where' 	=> "template_id = '$templateID'"
			);
	
		$ds->setup($this->_db,$sql);
		$result = $ds->toArray();
		if (!empty($result)) return $result[0];
		return $result;		
	}
}
?>
